package org.uim.cricket.analyzer.gui;

import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.DefaultListModel;
import javax.swing.UIManager;

import org.uim.cricket.entities.aggr.SingleInning;
import org.uim.cricket.entities.batting.BattingInning;
import org.uim.cricket.entities.match.CricketMatch;
import org.uim.cricket.entities.partnerships.FallOfWicket;

/**
 *
 * @author Isuru Madushanka
 */
public class AskRetiredComeBacks extends javax.swing.JDialog {

	private static final long serialVersionUID = 27926416194214134L;
	
	private CricketMatch match = null;
	private SingleInning inning = null;
	private FallOfWicket fow = null;
	private BattingInning batinn = null;
	
	/** Creates new form AskRetiredHurtBatsmen */
    public AskRetiredComeBacks(java.awt.Frame parent, boolean modal,
    		CricketMatch match, SingleInning inning, FallOfWicket fow, BattingInning binn) {
        super(parent, modal);
        
        this.match = match;
        this.fow = fow;
        this.inning = inning;
        this.batinn = binn;
        
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        
        initComponents();

        this.setCenterScreen();
        this.fillDetails();
    }

    private void fillDetails() {
    	String id = this.match.getId().getPrefix() + "#" + this.match.getId().getInternationalID();
        this.setTitle("Retired-hurt Batsmen : " + id);

        this.lblMatchID.setText(id);
        this.lblInning.setText(inning.getInningMatch() + "");
        this.lblBatTeam.setText(inning.getBattedTeam());

        DefaultListModel model = new DefaultListModel();
        model.addElement(batinn.getBatsman().getTitleName() + "[" + batinn.getOutDetails().getOutType().toString() + "]");
        this.lstBatsmen.setModel(model);
        this.lstBatsmen.setSelectedIndex(0);
        
        this.lblDetails.setText(model.size() + " player(s) found retired but their final out state is retired hurt.");
    }

    private void setCenterScreen() {
        Dimension window = this.getSize();
        Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();

        int newX = (int) (screen.getWidth() - window.getWidth()) / 2;
        int newY = (int) (screen.getHeight() - window.getHeight()) / 2;

        this.setLocation(newX, newY);
    }
    
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        lblMatchID = new javax.swing.JLabel();
        lblInning = new javax.swing.JLabel();
        lblBatTeam = new javax.swing.JLabel();
        lblDetails = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        lstBatsmen = new javax.swing.JList();
        lblArrived = new javax.swing.JLabel();
        jComboBox1 = new javax.swing.JComboBox();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Retired Hurt Batsmen");
        setResizable(false);

        jLabel1.setText("Match ID:");

        jLabel2.setText("Inning:");

        jLabel3.setText("Batting Team:");

        lblMatchID.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        lblMatchID.setText("#match-id");

        lblInning.setText("0");

        lblBatTeam.setText("team-batted");

        lblDetails.setText("0 players found retired but their final out state is retired hurt.");

        lstBatsmen.setModel(new javax.swing.AbstractListModel() {
			private static final long serialVersionUID = 1L;
			String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        lstBatsmen.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                lstBatsmenValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(lstBatsmen);

        lblArrived.setText("p arrived to on fall of wicket:");

        jComboBox1.setMaximumRowCount(15);
        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" }));
        jComboBox1.setSelectedIndex(1);
        jComboBox1.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jComboBox1ItemStateChanged(evt);
            }
        });
        jComboBox1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox1ActionPerformed(evt);
            }
        });

        jButton1.setText("OK");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(lblArrived)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(lblDetails)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3))
                        .addGap(26, 26, 26)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblBatTeam)
                            .addComponent(lblInning)
                            .addComponent(lblMatchID)))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 395, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(19, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(lblMatchID))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(lblInning))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(lblBatTeam))
                .addGap(26, 26, 26)
                .addComponent(lblDetails)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblArrived)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jButton1)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
        this.setVisible(false);
    }

    private void lstBatsmenValueChanged(javax.swing.event.ListSelectionEvent evt) {
        String sel = (this.lstBatsmen.getSelectedValue().toString());
        int selIndex = this.jComboBox1.getSelectedIndex();
        
        int pos = sel.indexOf("[");
        String selBatsman = sel;

        if (pos > 0) {
             selBatsman = sel.substring(0, pos).trim();
        }
        
        this.fow.setRetiredComeBackWicket(selIndex);
        this.lblArrived.setText(selBatsman + " arrived to on fall of wicket:");
    }

    public FallOfWicket getFallOfWicket() {
    	return this.fow;
    }
    
    private void jComboBox1ItemStateChanged(java.awt.event.ItemEvent evt) {

    }

    private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {
        int index = this.jComboBox1.getSelectedIndex();
        this.fow.setRetiredComeBackWicket(index);
    }

    // Variables declaration - do not modify
    private javax.swing.JButton jButton1;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblArrived;
    private javax.swing.JLabel lblBatTeam;
    private javax.swing.JLabel lblDetails;
    private javax.swing.JLabel lblInning;
    private javax.swing.JLabel lblMatchID;
    private javax.swing.JList lstBatsmen;
    // End of variables declaration

}
